<?php
namespace app\admin\controller;
use \think\Db;
use think\Cache;
use think\Loader;
use think\Session;
use think\Controller;
/**
 * Class Member 员工管理控制器
 * @package app\admin\controller
 */
class Staff extends Controller
{       
    //员工列表
    public function index()
    {
    	$param = $this->request->param();
    	$where = [];
        //条件查询
        $userName   = empty($param['userName']) ? '' : $param['userName'];
        if (!empty($userName)) {
            $where['a.userName'] = $userName;
        }
        $type   = empty($param['userPhone'])? '' : $param['userPhone'];
        if (!empty($type)) {
            $where['a.type'] = $type;
        }
        $type   = empty($param['type'])? '' : $param['type'];
        if (!empty($type)) {
            $where['a.type'] = $type;
        }
    	$rs=db::name('staff')->alias('a')->join('mj_shop b','a.shopId=b.shopId','left')->field('a.*,b.shopName')->where($where)->paginate(10);
    	$count=db::name('staff')->where($where)->count();
    	$this->assign("count", $count);
        $this->assign("filter", $param);//设置过滤条件
    	$this->assign('arr',$rs);
        return $this->fetch();
    }

    //新增员工
    public function add(){

        return $this->fetch();
    }

    //新增员工提交
    public function addPost(){
       $param = $this->request->param();
       //dump($param);exit;
       $arr=[
            'userName'    =>$param['userName'],
            'userPhone'     =>$param['userPhone'],
            'userPass'      =>st_password($param['userPass']),
            'userNumber'    =>'qj'.mt_rand('1000','9999'),
            'userSex'       =>$param['userSex'],
            'type'          =>$param['type'],
            'addtime'       =>date('Y-m-d H:i:s')
       ];
       $rs=db::name('staff')->insert($arr);
       if($rs){
            return json(['code'=>200,'msg'=>'添加成功！']);
       }else{
            return json(['code'=>400,'msg'=>'添加失败！']);
       }

    }

    //修改商店
    public function edit($userId){
        $arr=db::name('staff')->where('userId',$userId)->find();

        $this->assign('arr',$arr);
        $this->assign('userId',$userId);
        return $this->fetch();
    }

    //新增商店提交
    public function editPost(){
       $param = $this->request->param();
       //dump($param);exit;

       $arr=[
            'userName'      =>$param['userName'],
            'userPhone'     =>$param['userPhone'],
            'userSex'       =>$param['userSex'],
            'type'          =>$param['type'],
       ];
       if(!empty($param['userPass'])){
            $arr['userPass']=st_password($param['userPass']);
       }
       $rs=db::name('staff')->where('userId',$param['userId'])->update($arr);
       if($rs){
            return json(['code'=>200,'msg'=>'修改成功！']);
       }else{
            return json(['code'=>400,'msg'=>'修改失败！']);
       }

    }

    //删除
    public function del($id)
    {
        $result = Db::name('staff')->where('userId', $id)->delete();
        if($result){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else {
            return json(['code'=>400,'msg'=>'删除失败！']);
        }
    }


    //改变个人会员状态
    public function changeState()
    {
        $userId = input('id');
        $userStatus = input('status');
        $userStatus = $userStatus==1?'2':'1';
        $result = Db::name('shop_config')->where('id', $userId)->update(['status' => $userStatus]);
        if($result){
            return json(['code'=>1,'msg'=>'状态设置成功！']);
        }else {
            return json(['code'=>1,'msg'=>'状态设置失败！']);
        }
    }
}